package com.das.mechanic_base.adapter.processnew;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.alone.X3CameraNewAdapter;
import com.das.mechanic_base.bean.alone.AloneImageAndRecordBean;
import com.das.mechanic_base.bean.alone.SpeechBean;
import com.das.mechanic_base.bean.processsive.ProcessNewBean;
import com.das.mechanic_base.bean.processsive.ProgressListBean;
import com.das.mechanic_base.utils.AverageGapItemDecoration;
import com.das.mechanic_base.utils.LanguageUtiles;
import com.das.mechanic_base.utils.X3EdittextWatcher;
import com.das.mechanic_base.utils.X3FileUtils;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.das.mechanic_base.widget.X3AloneSoundView;
import com.google.gson.Gson;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.kproduce.roundcorners.RoundLinearLayout;
import com.kproduce.roundcorners.RoundTextView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class NewProcessShowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private SpeechRecognizer mIat;
    private boolean isShowRequired;
//    private List<ProcessSnBean.ProcedureTempletEntityListBean> mList;
    private List<ProgressListBean> mList;

    private boolean isHaveProcess,isHaveShort;
    /**
     * 0 首次进入
     * 1 定位失败
     * 2 定位成功
     * 3 谷歌定位失败
     */
    private int showLocation;
    private String evaluaFile;

    public NewProcessShowAdapter(Context context) {
        this.context = context;
        mList = new ArrayList<>();
    }

    public void changeShowRequired(boolean isShowRequired) {
        this.isShowRequired = isShowRequired;
        notifyDataSetChanged();
    }

    public List<ProgressListBean> getmList() {
        return mList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == 0){
            return new NewProcessShowAdapter.MyProcessShowViewHolder(LayoutInflater.from(context).inflate(R.layout.new_process_show_adapter, parent, false));

        } else if(viewType == 1){ //施工过程
            return new NewProcessShowAdapter.MyProcessShowMorePhotoViewHolder(LayoutInflater.from(context).inflate(R.layout.new_process_show_adapter_more_photo, parent, false));

        } else if(viewType == 2){ //履行保养计划，可填可不填
            return new NewProcessShowAdapter.MyProcessShowPlanViewHolder(LayoutInflater.from(context).inflate(R.layout.new_process_show_adapter_plan, parent, false));

        }
        //技师评论
        return new NewProcessShowAdapter.MyProcessCommentHolder(LayoutInflater.from(context).inflate(R.layout.new_process_show_adapter_comment, parent, false));

    }

    public void changeLocation(List<ProgressListBean> mList,int showLocation) {
        this.showLocation = showLocation;
        this.mList = mList;
        notifyItemChanged(0);
    }

    public void changeLocation(int showLocation) {
        this.showLocation = showLocation;
        notifyItemChanged(0);
    }

    /**
     * @param strings
     */
    public void changeDate(List<ProgressListBean> strings,int refreshIndex) {
        this.mList = strings;
        notifyItemChanged(refreshIndex);
    }

    /**
     * 封装的数据  num网络数据的条数
     * @param strings
     */
    public void changeDate(List<ProgressListBean> strings,boolean isHaveProcess,boolean isHaveShort) {
        this.mList = strings;
        this.isHaveProcess = isHaveProcess;
        this.isHaveShort = isHaveShort;
        notifyDataSetChanged();
    }

    /**
     * 获取当前题目的sn
     * @return
     */
//    public String getServiceSn(int index) {
//        return mList.get(index).getSn();
//    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);

        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
        if (position == mList.size() - 1) {
            layoutParams.bottomMargin = X3ScreenUtils.dipToPx(28,context);
        }else {
            layoutParams.bottomMargin = 0;
        }
        holder.itemView.setLayoutParams(layoutParams);

        if(viewType == 0) {
            MyProcessShowViewHolder newholder = (MyProcessShowViewHolder) holder;

            if (position == 0) {
                newholder.itemView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
            }else {
                changeShowRequired(newholder);
            }

            //newholder.itemView.setVisibility(position == 0 ? View.VISIBLE : (isShowRequired ? View.GONE : View.VISIBLE));

            newholder.tv_title.setText(mList.get(position).title);

            newholder.tv_star.setText(position == 0 ? context.getString(R.string.x3_must_input) : context.getString(R.string.x3_no_must_input));

            newholder.tv_star.setBackgroundColor(position == 0 ? Color.parseColor("#100077ff") : ContextCompat.getColor(context,R.color.bg_F0F0F5));

            newholder.tv_star.setTextColor(position == 0 ? Color.parseColor("#0077ff") : Color.parseColor("#b1b3bd"));

            String location = mList.get(position).location;

            if (position == 0) {
                newholder.ll_location.setVisibility(View.VISIBLE);
                newholder.tv_refresh.setVisibility(View.GONE);
                newholder.ll_location.setVisibility((showLocation == 0) ? View.GONE : View.VISIBLE);
                newholder.tv_refresh.setVisibility((showLocation == 1) ? View.VISIBLE : View.GONE);
                newholder.tv_location.setText((showLocation == 1) ? context.getString(R.string.get_location) : mList.get(position).location + "");
                newholder.tv_location.setOnClickListener(v -> {
                    if (iOnClickTake != null) {
                        iOnClickTake.iOnGetLocation();
                    }
                });

                if (X3StringUtils.isEmpty(location)) {
                    if (iOnClickTake != null) {
                        iOnClickTake.iOnGetLocation();
                    }
                }else {
                    newholder.ll_location.setVisibility(View.VISIBLE);
                    newholder.tv_refresh.setVisibility( View.GONE);
                    newholder.tv_location.setText( mList.get(position).location + "");
                }

                newholder.tv_refresh.setOnClickListener(v -> {
                    // 获取定位
                    if (iOnClickTake != null) {
                        iOnClickTake.iOnGetLocation();
                    }
                });
            }else {
                newholder.ll_location.setVisibility(View.GONE);
            }

            newholder.rlv_photo.setLayoutManager(new GridLayoutManager(context, 3));

            List<AloneImageAndRecordBean> urlList = mList.get(position).urlList;
            if (X3StringUtils.isListEmpty(urlList)) {
                urlList = new ArrayList<>();
            }
            X3CameraNewAdapter newAdapter = new X3CameraNewAdapter(context,true,position,3);

            newholder.rlv_photo.setAdapter(newAdapter);
            newAdapter.changeData(urlList);
            newAdapter.setiOnItemCameraClick(new X3CameraNewAdapter.IOnItemCameraClick() {
                @Override
                public void iOnItemCameraClick(int titleIndex) {
                    if (iOnClickTake != null) {
                        iOnClickTake.iOnClickTakePicture(position,mList.get(position).title,-1,"",newAdapter.getItemCount() - 1,newAdapter.getImageList(),false);
                    }
                }

                @Override
                public void iOnDeleteClick(long resId, int mPosition, int titleIndex, String path) {
                    if (iOnClickTake != null) {
                        iOnClickTake.iOnClickRemovePicture(position,mList.get(position).title,-1,"",newAdapter.getItemCount() - 1,mPosition,false);
                    }
                }

                @Override
                public void iOnAgainUploadImage(String path, int titleIndex, int imageIndex) {
                    if (iOnClickTake != null) {
                        iOnClickTake.iOnAgainUploadImage(path,titleIndex,mList.get(position).title,-1,"",newAdapter.getItemCount() - 1,newAdapter.getImageList(),false);
                    }
                }

                @Override
                public void iOnDeleteImage(int titleIndex, List<AloneImageAndRecordBean> mList) {
                    if (iOnClickTake != null) {
                        iOnClickTake.iOnDeleteImage(titleIndex,mList,"",false);
                    }
                }
            });

        } else if(viewType == 1){
            //施工过程
            MyProcessShowMorePhotoViewHolder  newholder = (MyProcessShowMorePhotoViewHolder) holder;
            newholder.tv_title.setText(mList.get(position).title);
            newholder.rlv_photo.setLayoutManager(new LinearLayoutManager(context));
            List<ProcessNewBean.ProcedureTempletEntityListDTO> pList = mList.get(position).pList;

            // 检查是否有必填项目
            List<List<AloneImageAndRecordBean>> pImageList = mList.get(position).pImageList;
            boolean isHave = false;
            if(X3StringUtils.isListEmpty(pList)){
                pList = new ArrayList<>();

                newholder.tv_star.setText(context.getString(R.string.x3_no_must_input));

                newholder.tv_star.setBackgroundColor(ContextCompat.getColor(context,R.color.bg_F0F0F5));

                newholder.tv_star.setTextColor(Color.parseColor("#b1b3bd"));

                changeShowRequired(newholder);
            }else {

                for (int i = 0; i < pList.size(); i++) {
                    boolean needPhoto = pList.get(i).needPhoto;
                    if (needPhoto) {
                        isHave = true;
                        break;
                    }
                }

                newholder.tv_star.setText(isHave ? context.getString(R.string.x3_must_input) : context.getString(R.string.x3_no_must_input));

                newholder.tv_star.setBackgroundColor(isHave ? Color.parseColor("#100077ff") : ContextCompat.getColor(context,R.color.bg_F0F0F5));

                newholder.tv_star.setTextColor(isHave ? Color.parseColor("#0077ff") : Color.parseColor("#b1b3bd"));

                if (!isHave) {
                    changeShowRequired(newholder);
                }else {
                    holder.itemView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
                }
            }

            ((MyProcessShowMorePhotoViewHolder) holder).rll_add_process.setOnClickListener(view ->{
                if (iOnClickCustomProcess != null){
                    iOnClickCustomProcess.iOnClickAddProcess();
                }
            });

            if(X3StringUtils.isListEmpty(pImageList)){
                pImageList = new ArrayList<>();
            }

            if (pList != null && pList.size()>0){
                ProcessNewBean.ProcedureTempletEntityListDTO dto = pList.get(0);
                if (dto != null){
                    if (X3StringUtils.isEmpty(dto.name)){
                        pList.remove(0);
                    }
                }
            }

            ConstructionProcessAdapter constructionProcessAdapter = new ConstructionProcessAdapter(pList,pImageList,context);
            newholder.rlv_photo.setAdapter(constructionProcessAdapter);
            constructionProcessAdapter.changeRequiredShow(isShowRequired);
            constructionProcessAdapter.setIOnClickTitle(iOnClickTitle);
            constructionProcessAdapter.setShowNotNeedPhoto(isHave);
            constructionProcessAdapter.setiOnClickPhoto(new ConstructionProcessAdapter.IOnClickPhoto() {
                @Override
                public void iOnClickTakePicture(int imageIndex, String imageTitle, int imageNum,List<AloneImageAndRecordBean> imgList) {
                    if (iOnClickTake != null) {
                        iOnClickTake.iOnClickTakePicture(position,mList.get(position).title,imageIndex,imageTitle,imageNum,imgList,true);
                    }
                }

                @Override
                public void iOnClickRemovePicture(int imageIndex, String imageTitle, int num, int mPosition) {
                    if (iOnClickTake != null) {
                        iOnClickTake.iOnClickRemovePicture(position,mList.get(position).title,imageIndex,imageTitle,num,mPosition,true);
                    }
                }

                @Override
                public void iOnAgainUploadImage(String path, int titleIndex,String imageName) {
                    if (iOnClickTake != null) {
                        iOnClickTake.iOnAgainUploadImage(path,position,mList.get(position).title,titleIndex,imageName,0,new ArrayList<>(),true);
                    }
                }

                @Override
                public void iOnDeleteImage(int titleIndex, List<AloneImageAndRecordBean> mList,String imageName) {
                    if (iOnClickTake != null) {
                        iOnClickTake.iOnDeleteImage(titleIndex,mList,imageName,true);
                    }
                }
            });

        }else if(viewType == 2){
            MyProcessShowPlanViewHolder newholder = (MyProcessShowPlanViewHolder) holder;

            changeShowRequired(newholder);

            newholder.tv_star.setText(context.getString(R.string.x3_no_must_input));

            newholder.tv_star.setBackgroundColor(ContextCompat.getColor(context,R.color.bg_F0F0F5));

            newholder.tv_star.setTextColor(Color.parseColor("#b1b3bd"));

            newholder.rlv_choice.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL,false){
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            List<ProcessNewBean.ServiceBaseShortBean> serviceBaseShortList = mList.get(position).serviceBaseShortList;
            if(X3StringUtils.isListEmpty(serviceBaseShortList)){
                serviceBaseShortList = new ArrayList<>();
            }

            newholder.tv_title.setText(mList.get(position).title);
            ConstructionProcessPlanAdapter constructionProcessPlanAdapter = new ConstructionProcessPlanAdapter(serviceBaseShortList,context);
            newholder.rlv_choice.setAdapter(constructionProcessPlanAdapter);

            constructionProcessPlanAdapter.setiOnClickChoice((sList) -> {
                if(iOnClickPlan != null){
                    iOnClickPlan.iOnClickSelectPlan(sList);
                }

            });

        }else {
            MyProcessCommentHolder commentHolder = (MyProcessCommentHolder) holder;

            changeShowRequired(commentHolder);

            commentHolder.tv_title.setText(mList.get(position).title + "");
            commentHolder.et_input.setText(mList.get(position).evaluation);
            commentHolder.tv_star.setText(context.getString(R.string.x3_no_must_input));

            commentHolder.tv_star.setBackgroundColor(ContextCompat.getColor(context,R.color.bg_F0F0F5));

            commentHolder.tv_star.setTextColor(Color.parseColor("#b1b3bd"));
            commentHolder.tv_sound.setOnClickListener(view -> {
                XXPermissions.with(context)
                        .permission(Permission.RECORD_AUDIO)
                        .permission(Permission.Group.STORAGE)
                        .request(new OnPermissionCallback() {
                            @Override
                            public void onGranted(List<String> permissions, boolean all) {
                                if(all){
                                    startSoundEdit( commentHolder.et_input,commentHolder.ll_start,commentHolder.tv_sound,commentHolder.as_view,commentHolder.tv_end);
                                    commentHolder.tv_sound.setVisibility(View.GONE);
                                    commentHolder.ll_start.setVisibility(View.VISIBLE);
                                    commentHolder.as_view.startAnim();
                                    if(iOnClickPlan != null){
                                        iOnClickPlan.iOnShowButton(false);
                                        iOnClickPlan.onScrollBottom();

                                    }
                                }else {
                                    XXPermissions.startPermissionActivity(context,permissions);
                                }
                            }

                            @Override
                            public void onDenied(List<String> permissions, boolean never) {
                                XXPermissions.startPermissionActivity(context,permissions);
                            }
                        });
            });

            commentHolder.et_input.addTextChangedListener(new X3EdittextWatcher() {
                @Override
                protected void textInputAfter(String content) {
                    if(iOnClickPlan != null){
                        iOnClickPlan.iOnClickComment(content);
                        ProgressListBean progressListBean = mList.get(position);
                        progressListBean.evaluation = content;
                        mList.remove(position);
                        mList.add(position,progressListBean);
                    }
                }
            });
        }

    }

    /**
     * 控制是否显示必填项
     * @param newholder
     */
    private void changeShowRequired(RecyclerView.ViewHolder newholder) {
        ViewGroup.LayoutParams itemLayout = newholder.itemView.getLayoutParams();
        if (isShowRequired) {
            itemLayout.height = 0;
        }else {
            itemLayout.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        }
        newholder.itemView.setLayoutParams(itemLayout);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 || position == 1 || position == 2 || position == (!isHaveProcess ? 3 : 4) || position == (!isHaveProcess ? 4 : 5)) {
            return 0;
        }else if (isHaveProcess && position == 3) {
            return 1;
        }else if ((position == (!isHaveProcess ? 5 : 6)) && isHaveShort) {
            return 2;
        }
        return 3;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyProcessShowViewHolder extends RecyclerView.ViewHolder {

        private RecyclerView rlv_photo;
        private TextView tv_title;
        private LinearLayout ll_location;
        private TextView tv_location;
        private TextView tv_refresh;
        private RoundTextView tv_star;
        public MyProcessShowViewHolder(@NonNull View itemView) {
            super(itemView);
            rlv_photo = itemView.findViewById(R.id.rlv_photo);
            tv_title = itemView.findViewById(R.id.tv_title);
            ll_location = itemView.findViewById(R.id.ll_location);
            tv_location = itemView.findViewById(R.id.tv_location);
            tv_refresh = itemView.findViewById(R.id.tv_refresh);
            tv_star = itemView.findViewById(R.id.tv_star);
        }
    }

    class MyProcessShowMorePhotoViewHolder extends RecyclerView.ViewHolder {

        private RecyclerView rlv_photo;
        private TextView tv_title;
        private RoundTextView tv_star;
        private RoundLinearLayout rll_add_process;
        public MyProcessShowMorePhotoViewHolder(@NonNull View itemView) {
            super(itemView);
            rlv_photo = itemView.findViewById(R.id.rlv_photo);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_star = itemView.findViewById(R.id.tv_star);
            rll_add_process = itemView.findViewById(R.id.rll_add_process);
        }
    }
    class MyProcessShowPlanViewHolder extends RecyclerView.ViewHolder {

        private RecyclerView rlv_choice;
        private TextView tv_title;
        private RoundTextView tv_star;
        public MyProcessShowPlanViewHolder(@NonNull View itemView) {
            super(itemView);
            rlv_choice = itemView.findViewById(R.id.rlv_choice);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_star = itemView.findViewById(R.id.tv_star);
        }
    }

    class MyProcessCommentHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        EditText et_input;
        TextView tv_sound;
        LinearLayout ll_start;
        X3AloneSoundView as_view;
        TextView tv_end;
        RoundTextView tv_star;
        public MyProcessCommentHolder(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            et_input = itemView.findViewById(R.id.et_input);
            tv_sound = itemView.findViewById(R.id.tv_sound);
            ll_start = itemView.findViewById(R.id.ll_start);
            as_view = itemView.findViewById(R.id.as_view);
            tv_end = itemView.findViewById(R.id.tv_end);
            tv_star = itemView.findViewById(R.id.tv_star);
        }
    }

    public void destoryIat(){
        if(mIat != null){
            mIat.destroy();
        }
    }
    public boolean isListening(){
        if(mIat != null && mIat.isListening()){
            return true;
        }
        return false;
    }

    public interface IOnClickPlan{

        void iOnClickSelectPlan(List<ProcessNewBean.ServiceBaseShortBean> shortBeans);
        void iOnClickComment(String comment);
        void iOnClickVoice(File evaluaFile);
        void onScrollBottom();
        void iOnShowButton(boolean isshow);

    }
    IOnClickPlan iOnClickPlan;

    public void setiOnClickPlan(IOnClickPlan iOnClickPlan) {
        this.iOnClickPlan = iOnClickPlan;
    }

    // 施工过程，自定义工序
    public interface IOnClickCustomProcess{
        void iOnClickAddProcess();
    }
    IOnClickCustomProcess iOnClickCustomProcess;
    public void setIOnClickCustomProcess(IOnClickCustomProcess iOnClickCustomProcess) {
        this.iOnClickCustomProcess = iOnClickCustomProcess;
    }

    ConstructionProcessAdapter.IOnClickTitle iOnClickTitle;
    public void setIOnClickTitle(ConstructionProcessAdapter.IOnClickTitle iOnClickTitle) {
        this.iOnClickTitle = iOnClickTitle;
    }

    IOnClickTake iOnClickTake;

    public void setiOnClickTake(IOnClickTake iOnClickTake) {
        this.iOnClickTake = iOnClickTake;
    }

    public interface IOnClickTake {
        /**
         * 点击拍照
         * @param index
         */
        void iOnClickTakePicture(int index,String title,int imageIndex,String imageTitle,int imageNum,List<AloneImageAndRecordBean> imgList,boolean isWork);
        /**
         * 点击删除图片

         */
        void iOnClickRemovePicture(int index,String title, int imageIndex,String imageTitle,int imageNum,int mPosition,boolean isWork);

        /**
         * 点击重新上传图片
         * @param path
         * @param titleIndex
         */
        void iOnAgainUploadImage(String path,int titleIndex,String title,int imageIndex,String imageTitle,int imageNum,List<AloneImageAndRecordBean> imgList,boolean isWork);

        /**
         * 之前没有上传成功 则直接删除
         * @param titleIndex
         * @param
         */
        void iOnDeleteImage(int titleIndex, List<AloneImageAndRecordBean> mList,String imageName,boolean isWork);

        void iOnGetLocation();
    }

    /**
     * 语音听写
     * @param et_input
     * @param ll_start
     * @param tv_sound
     * @param as_view
     * @param tv_end
     */
    private void startSoundEdit(EditText et_input, LinearLayout ll_start, TextView tv_sound, X3AloneSoundView as_view, TextView tv_end) {
        evaluaFile = X3FileUtils.getStoragePath(context, X3FileUtils.RECORD_STORAGE_PATH) + System.currentTimeMillis()+"evalua.wav";
        File file = new File(evaluaFile);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        mIat = SpeechRecognizer.createRecognizer(context, i -> Log.e("SSSS", "初始化：" + i));

        //设置语法ID和 SUBJECT 为空，以免因之前有语法调用而设置了此参数；或直接清空所有参数，具体可参考 DEMO 的示例。
        mIat.setParameter(SpeechConstant.CLOUD_GRAMMAR, null);
        mIat.setParameter(SpeechConstant.SUBJECT, null);
        //设置返回结果格式，目前支持json,xml以及plain 三种格式，其中plain为纯听写文本内容
        mIat.setParameter(SpeechConstant.RESULT_TYPE, "json");

        //此处engineType为“cloud”
        mIat.setParameter(SpeechConstant.ENGINE_TYPE, "cloud");
        //设置语音输入语言，zh_cn为简体中文
        if (LanguageUtiles.isZhRCN()) {
            mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        }else {
            mIat.setParameter(SpeechConstant.LANGUAGE, "en_us");
        }
        //设置结果返回语言
        mIat.setParameter(SpeechConstant.ACCENT, "mandarin");
        // 设置语音前端点:静音超时时间，单位ms，即用户多长时间不说话则当做超时处理
        //取值范围{1000～10000}
        mIat.setParameter(SpeechConstant.VAD_BOS, "10000");
        //设置语音后端点:后端点静音检测时间，单位ms，即用户停止说话多长时间内即认为不再输入，
        //自动停止录音，范围{0~10000}
        mIat.setParameter(SpeechConstant.VAD_EOS, "10000");
        //设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
        mIat.setParameter(SpeechConstant.ASR_PTT, "1");

        mIat.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
        mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH, evaluaFile);

        //开始识别，并设置监听器
        mIat.startListening(new RecognizerListener() {
            @Override
            public void onVolumeChanged(int i, byte[] bytes) {

            }

            @Override
            public void onBeginOfSpeech() {
            }

            @Override
            public void onEndOfSpeech() {
                ll_start.setVisibility(View.GONE);
                tv_sound.setVisibility(View.VISIBLE);
                as_view.stopAnim();

            }

            @Override
            public void onResult(RecognizerResult recognizerResult, boolean b) {
                SpeechBean speechBean = new Gson().fromJson(recognizerResult.getResultString(), SpeechBean.class);
                List<SpeechBean.WsBean> ws = speechBean.getWs();
                String strContent = "";
                for (SpeechBean.WsBean wsBean : ws) {
                    strContent = strContent + wsBean.getCw().get(0).getW();
                }
                et_input.setText(et_input.getText().toString() + strContent);
            }

            @Override
            public void onError(SpeechError speechError) {

            }

            @Override
            public void onEvent(int i, int i1, int i2, Bundle bundle) {

            }
        });

        tv_end.setOnClickListener(view -> {
            if(iOnClickPlan != null){
                iOnClickPlan.iOnShowButton(true);
            }
            tv_sound.setVisibility(View.VISIBLE);
            ll_start.setVisibility(View.GONE);

            if (mIat == null) {
                X3ToastUtils.showMessage(context.getResources().getString(R.string.x3_speech_init_error) + "！");
                return;
            }
            mIat.stopListening();
            if(iOnClickPlan != null){
                File files = new File(evaluaFile);
                if(files.length() == 0){
                    new Handler(){}.postDelayed(() -> iOnClickPlan.iOnClickVoice(file), 1000);
                }else {
                    iOnClickPlan.iOnClickVoice(file);
                }
            }

        });

    }
}

